package org.adultedu.controller;

import org.adultedu.pojo.Teacher;
import org.adultedu.result.Result;
import org.adultedu.result.ResultEnum;
import org.adultedu.result.ResultUtil;
import org.adultedu.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@CrossOrigin
@RestController
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    @GetMapping (value = "/queryteacher")
    public Result queryTeacher(String input,String value){
        Teacher teacher = new Teacher();
        int select = Integer.parseInt(value);
        System.out.println(input+select);
        switch (select){
            case 1:{
                teacher.setTname(input);
                System.out.println("ok");
                break;
            }
            case 2:{
                teacher.setTele(input);
                break;
            }
            case 3:{
                teacher.setEmail(input);
                break;
            }
            case 4:{
                teacher.setProfession(input);
                break;
            }
            case 5:{
                teacher.setStar(Integer.parseInt(input));
                break;
            }
            case 6:{
                teacher.setSfz(input);
                break;
            }
            case 7:{
                teacher.setTqc(input);
                break;
            }
            default:{
                return ResultUtil.success(teacherService.queryAllTeacher());
            }
        }
        System.out.println(teacher);
        return ResultUtil.success(teacherService.queryUniqueTeacher(teacher));
    }
    @GetMapping (value = "/getthisteacher")
    public Result getThisTeacher(String tid){
        Teacher t = new Teacher(tid,null,null,null,null,null,null,null,null,null,null,null);
        List list = teacherService.queryUniqueTeacher(t);
        if(!list.isEmpty()){
            return ResultUtil.success(list.get(0));
        }else {
            return ResultUtil.error(ResultEnum.DATA_IS_NULL.getCode(),ResultEnum.DATA_IS_NULL.getMsg());
        }
    }
    @GetMapping (value = "/getteacher")
    public Result getTeacher(String tname,String tele,Integer isverify){
        Teacher teacher = new Teacher();
        teacher.setTname(tname);
        teacher.setTele(tele);
        teacher.setIsverify(isverify);
        System.out.println(teacher);
        List list = teacherService.queryUniqueTeacher(teacher);
        if(!list.isEmpty()){
            return ResultUtil.success(list);
        }else {
            return ResultUtil.error(ResultEnum.DATA_IS_NULL.getCode(),ResultEnum.DATA_IS_NULL.getMsg());
        }
    }
    @GetMapping (value = "/getlimitteacher")
    public Result getLimitTeacher(String tname,String tele,Integer isverify,Integer currentpage,Integer pagesize){
        Integer length = teacherService.queryUniqueTeacher(new Teacher(null,tname,null,null,
                null,null,null,tele,
                null,null,null,isverify)).size();
        currentpage = (currentpage-1)*pagesize;
        List<Teacher> list = teacherService.queryLimitTeacher(tname,tele,isverify,currentpage,pagesize);
        if(!list.isEmpty()){
            return ResultUtil.success(list,length);
        }
        else {
            return ResultUtil.error(ResultEnum.DATA_IS_NULL.getCode(),ResultEnum.DATA_IS_NULL.getMsg());
        }

    }

    @RequestMapping(value= "/updateteacher",method = RequestMethod.POST)
    public Result updateTeacher(@RequestBody Map<String,Object> map){
        System.out.println(map);
        String tid = (String) map.get("tid");
        String tname = (String) map.get("tname");
        String edubgd = (String) map.get("edubgd");
        String profession = (String) map.get("profession");
        Integer star = (Integer) map.get("star");
        String tele = (String) map.get("tele");
        String email = (String) map.get("email");
        String sfz = (String) map.get("sfz");
        String tqc = (String) map.get("tqc");
        String sex = (String) map.get("sex");
        String intro= (String) map.get("intro");
        Integer isverify = (Integer) map.get("isverify");
        Teacher teacher = new Teacher(tid,tname,sex,edubgd,profession,star,intro,tele,email,sfz,tqc,isverify);
        System.out.println(teacher);
        if(teacherService.updateTeacher(teacher) == 1){
            return ResultUtil.success(ResultEnum.SUCCESS);
        }else {
            return ResultUtil.error(ResultEnum.UPDATE_FAIL.getCode(),ResultEnum.UPDATE_FAIL.getMsg());
        }
    }
    @RequestMapping(value= "/verifyteacher",method = RequestMethod.POST)
    public Result verifyTeacher(@RequestBody String[] list){
        for(int i=0;i<list.length;i++){
            System.out.print(list[i]);
        }
        int num = 0;
        Teacher teacher = new Teacher();
        for(int i=0;i<list.length;i++) {
            teacher.setTid(list[i]);
            teacher.setIsverify(1);
            if(teacherService.updateTeacher(teacher) == 1){
                num++;
            }
        }
        if(num == list.length){
            return ResultUtil.success(ResultEnum.SUCCESS);
        }else{
            return ResultUtil.error(ResultEnum.UPDATE_FAIL.getCode(),ResultEnum.UPDATE_FAIL.getMsg());
        }
    }

    @GetMapping(value = "/verifysingeteacher")
    public Result verifySingleTeacher(String tid){
        System.out.print(tid);
        Teacher teacher = new Teacher();
        teacher.setTid(tid);
        teacher.setIsverify(1);
        if(teacherService.updateTeacher(teacher) == 1){
            return ResultUtil.success(ResultEnum.SUCCESS);
        }else {
            return ResultUtil.error(ResultEnum.UPDATE_FAIL.getCode(),ResultEnum.UPDATE_FAIL.getMsg());
        }
    }

    @RequestMapping(value= "/deleteteacher",method = RequestMethod.POST)
    public Result deleteTeacher(@RequestBody String[] list) {
        int num = 0;
        for(int i=0;i<list.length;i++) {
            if(teacherService.deleteTeacher(list[i]) == 1){
                num++;
            }
        }
        if(num == list.length){
            return ResultUtil.success(ResultEnum.SUCCESS);
        }else{
            return ResultUtil.error(ResultEnum.DELETE_FAIL.getCode(),ResultEnum.DELETE_FAIL.getMsg());
        }
    }
}
